package decorator.inner;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author: zhouyang
 * @descrpition: 实现装饰者的io 读文件全转大写
 * @time: 2019-05-24 19:40
 */
public class UpperCaseInputStream extends FilterInputStream {
    /**
     * Creates a <code>FilterInputStream</code>
     * by assigning the  argument <code>in</code>
     * to the field <code>this.in</code> so as
     * to remember it for later use.
     *
     * @param in the underlying input stream, or <code>null</code> if
     *           this instance is to be created without an underlying stream.
     */
    //加入一个装饰者
    protected UpperCaseInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read() throws IOException {
        //调用超类的read 也就是FilterInputStream的read 这取决于传入何种类型的实现类
        int c = super.read();
        //如果没有结尾
        return c == -1 ? c: Character.toUpperCase((char)c);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int result = super.read(b, off, len);
        for(int i=0; i<result; i++){
            b[i] = (byte)Character.toUpperCase((char)(b[i]));
        }
        return result;
    }
}
